<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/7/16
 * Time: 10:21
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Member extends Base {
    //用户管理
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $map =array();
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $map['a.user_status'] =  array('neq',3);
            if(isset($keyword)&&$keyword!="")
            {
                $map['a.user_username|a.user_phone'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($sex)&&$sex!="")
            {
                $map['user_gender'] = $sex;
            }
            if(isset($grade)&&$grade!="")
            {
                $map['user_grade'] = $grade;
            }
            if(isset($subject)&&$subject!="")
            {
                $map['us.subject_id'] = $subject;
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.user_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.user_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.user_time'] = ['between time',[$start,$end]];
            }


            $map['user_type'] =1;
            $map['user_status'] =array('neq',5);
            $count = Db::table('up_u_user')->alias('a')
                ->join('up_area ua','ua.district_id = a.user_sheng','left')
                ->join('up_area ua1','ua1.district_id = a.user_shi','left')
                ->join('up_area ua2','ua2.district_id = a.user_qu','left')
                ->join('up_grade ug','ug.grade_id = a.user_grade','left')
                ->join('up_subject us','us.subject_id = a.user_subject','left')
                ->where($map)
                ->count();
            $list = Db::table('up_u_user')->alias('a')
                ->join('up_area ua','ua.district_id = a.user_sheng','left')
                ->join('up_area ua1','ua1.district_id = a.user_shi','left')
                ->join('up_area ua2','ua2.district_id = a.user_qu','left')
                ->join('up_grade ug','ug.grade_id = a.user_grade','left')
                ->join('up_subject us','us.subject_id = a.user_subject','left')
                ->where($map)
                ->field('a.*,ua.district,ua1.district as district1,
                ua2.district as district2,ug.grade_name,us.subject_name')
                ->page($page, $limit)
                ->order('a.user_id desc')
                ->select();//计算总页面
            foreach ($list as $key=>&$vo){
                $vo['address'] = $vo['district'].$vo['district1'].$vo['district2'];
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        $grade = Db::table('up_grade')->select();
        $subject = Db::table('up_subject')->order('subject_sort asc')->select();
        $this->assign('grade',$grade);
        $this->assign('subject',$subject);
        return $this->fetch();
    }

    //删除
    public function del(){
        $id = input('param.id');
        $res = Db::table('up_u_user')->where('user_id',$id)->setField('user_status',5);
        if($res ){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }

    /***
     * 冻结
     */
    public function frozen(){
        $ids = input('id');
        $res = Db::table('up_u_user')->where('user_id', $ids)->setField('user_status', 4);
        if($res){
            return json(['code' => 200, 'msg' => "操作成功"]);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    /***
     * 解冻
     */
    public function thaw(){
        $ids = input('id');
        $res = Db::table('up_u_user')->where('user_id',  $ids)->setField('user_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => "操作成功"]);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /***
     * 基本信息
     */
    public function basis(){
        $id = input('param.id');
        $info = Db::table('up_u_user')->where('user_id',$id)
            ->find();
        if($info['user_gender']==1){
            $info['user_gender_name'] = '男';
        }elseif ($info['user_gender']==2){
            $info['user_gender_name'] = '女';
        }
        //地区
        $user_sheng = Db::table('up_area')->where('district_id',$info['user_sheng'])->value('district');
        $user_shi = Db::table('up_area')->where('district_id',$info['user_shi'])->value('district');
        $user_qu = Db::table('up_area')->where('district_id',$info['user_qu'])->value('district');
        $info['address'] = $user_sheng.$user_shi.$user_qu;
        $this->assign('user',$info);
        return $this->fetch();
    }

    public function dk(){
        if(Request::instance()->isPost()){
            $id = input('param.id');
            $money = input('param.money');
            $res = Db::table('up_u_user')->where('user_id',$id)->setInc('user_balance',$money);
            $data['dk_user_id'] = session('uid');
            $data['dk_money'] = $money;
            $data['dk_time'] = date('Y-m-d H:i:s',time());
            $data['dk_status'] = 1;
            $data['user_id'] =$id;
            $data['dk_type'] =1;
            $data['dk_type_sex'] =1;
            Db::table('up_dk')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('param.user_id');
        $this->assign('id',$id);
        return $this->fetch();
    }
    public function kk(){
        if(Request::instance()->isPost()){
            $id = input('param.id');
            $money = input('param.money');
            $user_balance = Db::table('up_u_user')->where('user_id',$id)->value('user_balance');
            if($money>$user_balance){
                return json(['code' => 100, 'msg' => '余额不足,请重新输入']);
            }
            $res = Db::table('up_u_user')->where('user_id',$id)->setDec('user_balance',$money);
            $data['dk_user_id'] = session('uid');
            $data['dk_money'] = $money;
            $data['dk_time'] = date('Y-m-d H:i:s',time());
            $data['dk_status'] = 2;
            $data['user_id'] =$id;
            $data['dk_type'] =2;
            $data['dk_type_sex'] =1;
            Db::table('up_dk')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('param.user_id');
        $this->assign('id',$id);
        return $this->fetch();
    }
}